﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EmptyWallGallery.Core.BLL
{
    public class TagListView
    {
        public int TagId { get; set;}
        public string TagName { get; set; }
        public int? SiteId { get; set; }
    }

    public class Tags
    {
        public static Tags Instance
        {
            get { return new Tags(); }
        }

        public static TagListView LoadDataFromListItem(Tag item)
        {
            var objTags = new TagListView
            {
                TagId = item.TagID,
                TagName = item.TagName,
                SiteId = item.SiteID
            };
            return objTags;
        }

        /// <summary>
        /// Create tag
        /// </summary>
        /// <param name="tagName">Tag Name</param>
        /// <param name="siteId">Site Id</param>
        /// <returns></returns>
        public bool Create(string tagName, int siteId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objTags = new Tag
                    {
                        TagName = tagName,
                        SiteID = siteId
                    };

                    dc.Tags.InsertOnSubmit(objTags);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Tag
        /// </summary>
        /// <param name="tagId">Tag Id</param>
        /// <param name="tagName">Tag Name</param>
        /// <param name="siteId">Site Id</param>
        /// <returns></returns>
        public bool Update(int tagId, string tagName, int siteId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objTags = (dc.Tags.Where(p => p.TagID == tagId)).Single();
                    objTags.TagName = tagName;
                    objTags.SiteID = siteId;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete Tag
        /// </summary>
        /// <param name="tagId">Tag Id</param>
        /// <returns></returns>
        public bool Delete(int tagId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    dc.Justin_Tags_Delete(tagId);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Get List Tag
        /// </summary>
        /// <param name="tagId">Tag Id</param>
        /// <returns></returns>
        public List<TagListView> GetById(int tagId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = dc.Tags.Where(p => p.TagID == tagId).Select(p => new TagListView
                                                                                 {
                                                                                     TagId = p.TagID,
                                                                                     TagName = p.TagName,
                                                                                     SiteId = p.SiteID,
                                                                                 });

                return query.ToList();
            }
        }

        /// <summary>
        /// Get List Tag By Site
        /// </summary>
        /// <param name="siteId">Site Id</param>
        /// <returns></returns>
        public List<TagListView> GetBySite(int siteId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = dc.Tags.Where(p => p.SiteID == siteId).Select(p => new TagListView
                                                                                   {
                                                                                       TagId = p.TagID,
                                                                                       TagName = p.TagName,
                                                                                       SiteId = p.SiteID,
                                                                                   });

                return query.ToList();
            }
        }

        /// <summary>
        /// Get List Tag By Article
        /// </summary>
        /// <param name="articleId">Article Id</param>
        /// <returns></returns>
        public List<TagListView> GetByArticle(int articleId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = dc.TempArticleTags.Where(p => p.ArticleID == articleId).Select(p => new TagListView
                                                                                                   {
                                                                                                       TagId = Convert.ToInt32(p.TagsID),
                                                                                                   });
                return query.ToList();
            }
        }

        /// <summary>
        /// Write Tag Selected
        /// </summary>
        /// <param name="articleId">Article Id</param>
        /// <returns></returns>
        public string WriteTagSelect(int articleId)
        {
            var strOut = new StringBuilder();
            var reader = GetByArticle(articleId);

            strOut.AppendLine(string.Empty);
            strOut.AppendLine("<script language=\"javascript\" type=\"text/javascript\">");
            foreach (var tagListView in reader)
            {
                strOut.Append("gmobj(\"cboOption").Append(tagListView.TagId).Append("\").checked = true;");
                strOut.AppendLine(string.Empty);
            }
            strOut.AppendLine("</script>");
            return strOut.ToString();
        }

        /// <summary>
        /// Write Tag Inside
        /// </summary>
        /// <param name="siteId"></param>
        /// <param name="selectedId"></param>
        /// <returns></returns>
        public string WriteTagInside(int siteId, string selectedId = "")
        {
            var strOut = new StringBuilder();
            var reader = GetBySite(siteId);
            foreach (var tagListView in reader)
            {
                strOut.Append("<label><input type=\"checkbox\" id=\"cboOption").Append(tagListView.TagId).Append("\" name=\"cboOption\" value=\"").Append(tagListView.TagId).Append("\" /> ").Append(tagListView.TagName).Append("</label>");
            }
            return strOut.ToString();
        }
    }
}